package cn.izeno.wx

import cn.izeno.wx.models.component.*
import cn.izeno.wx.service.WX
import com.alibaba.fastjson.JSON
import io.reactivex.rxjava3.core.Observable
import org.junit.Before
import org.junit.Test
import org.slf4j.LoggerFactory
import java.lang.IllegalArgumentException

class WeappTemplateTest {
  val logger = LoggerFactory.getLogger(this::class.java.name)
  var err: Throwable? = null

  var appid: String = ""

  @Before
  fun setup() {
    Init
    appid = "wx1d403de848ddbc36"
  }

  @Test
  fun getDraftList() {
    val body = WX.open.weappDraftList().zzBody()
    logger.info(body.prettyJson())
  }

  @Test
  fun getTemplateList() {
    val body = WX.open.weappTemplateList().zzBody()
    logger.info(body.prettyJson())
  }

  @Test()
  fun addTemplate() {

    WX.open.weappDraftList()
        .map {
          it.data.firstOrNull()?.templateId
              ?: throw IllegalArgumentException("数据异常: " + JSON.toJSONString(it))
        }
        .flatMap { WX.open.weappAddTemplate(WeappDraftId(it)) }
        .doOnError { err = it }
        .subscribe { logger.info(it.prettyJson()) }

    assert(err == null)
  }

  @Test
  fun deleteTemplate() {
    WX.open.weappTemplateList()
        .map {
          it.data.firstOrNull()?.templateId
              ?: throw IllegalArgumentException("数据异常: " + JSON.toJSONString(it))
        }
        .flatMap {
          WX.open.weappDeleteTemplate(WeappTemplatetId(it))
        }
        .doOnError { err = it }
        .subscribe { logger.info(it.prettyJson()) }

    assert(err == null)
  }

  @Test
  fun weappCommit() {
    WX.open.weappTemplateList()
        .flatMap {

          val temp = it.data.firstOrNull() ?: error("没有可用的小程序模板")
          val commit = WeappCommit(
              templateId = temp.templateId,
              userDesc = temp.userDesc,
              userVersion = temp.userVersion
          )

          commit.extJson.extAppid = appid

          logger.warn(commit.prettyJson())

          WX.open.weappCommit(commit, APP_TOKEN)
        }
        .doOnError { err = it }
        .onErrorResumeNext { Observable.just(BaseRes()) }
        .subscribe { logger.info(it.prettyJson()) }
    assert(err == null)
  }

  @Test
  fun weappCategory() {
    val body = WX.open.weappCategory(APP_TOKEN).zzBody()
    logger.info(body.prettyJson())
  }

  @Test
  fun weappPages() {
    val body = WX.open.weappPages(APP_TOKEN).zzBody()
    logger.info(body.prettyJson())
  }

  // 提交审核
  @Test
  fun weappSubmit() {
    val page = WeappSubmitPage("pages/index/index", "首页")
    val submit = WeappSubmit(listOf(page))

    logger.warn(submit.prettyJson())
    WX.open.weappSubmitAudit(submit, APP_TOKEN)
        .doOnError { err = it }
        .subscribe { logger.info(it.prettyJson()) }
    assert(err == null)
  }


  // 所有可以选的分类
  @Test
  fun allCate() {
    val res = WX.open.weappAllCategory(APP_TOKEN).zzBody()
    logger.info(res.prettyJson())
  }


  // 添加类目
  @Test
  fun addCate() {
    val body = AddCategoryFC()
    val cate = AddCategoryFC.Category()
    cate.first = 304
    cate.second = 307
    body.categories = listOf(cate)

    val res = WX.open.weappAddCategory(body, APP_TOKEN).zzBody()
    logger.info(res.prettyJson())
  }


  // 一添加的类目
  @Test
  fun currentCate() {

    val res = WX.open.weappCategoryFC(APP_TOKEN).zzBody()
    logger.info(res.prettyJson())
  }

  @Test
  fun domain() {
    WX.open.weappModifyDomain(WeappDomain(), APP_TOKEN)
        .doOnError { err = it }
        .subscribe { logger.info(it.prettyJson()) }
    WX.open.weappModifyWebviewDomain(WeappDomain(), APP_TOKEN)
        .doOnError { err = it }
        .subscribe { logger.info(it.prettyJson()) }

    assert(err == null)
  }


  @Test
  fun sdkVersion() {
    val body = WX.open.weappSdkVersion(APP_TOKEN).zzBody()
    logger.info(body.prettyJson())
  }

  /**
   * 最新审核状态
   */
  @Test
  fun latestAudit() {
    WX.open.weappLatestAuditStatus(APP_TOKEN)
        .doOnError { err = it }
        .subscribe { logger.info(it.prettyJson()) }

    assert(err == null)
  }

  @Test
  fun bindTester() {
    val body = WX.open.weappBindTester(WeappTester.ofWechatId("puppet2436"), APP_TOKEN).zzBody()
    logger.info(body.prettyJson())
  }


  @Test
  fun unbindTester() {
    val body = WX.open.weappUnbindTester(WeappTester.ofWechatId("puppet2436"), APP_TOKEN).zzBody()
    logger.info(body.prettyJson())
  }

  @Test
  fun testerList() {
    val body = WX.open.weappTester(APP_TOKEN).zzBody()
    logger.info(body.prettyJson())
  }

}